package com.axapoint.opoc.core.dao;

import java.util.Collection;
import java.util.LinkedList;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.joda.time.DateTime;
import org.joda.time.Interval;

import com.anasoft.os.daofusion.bitemporal.WrappedBitemporalProperty;
import com.anasoft.os.daofusion.bitemporal.WrappedValueAccessor;

@Entity
@Table(name = "PLeave")
public class Leave extends ObjectRegistration<Leave, Leave.Value, Leave.Bitemporal> {
	private static final long serialVersionUID = 9168710745762860711L;

	@Entity
	@Table(name = "PLeaveValues")
	public static final class Value extends _Value<Leave, Bitemporal> {
		private static final long serialVersionUID = 1L;

		@SuppressWarnings("deprecation")
		@ManyToOne(fetch = FetchType.LAZY)
	    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	    @JoinColumn(name="engagementId")
		private Engagement engagement;

		@SuppressWarnings("deprecation")
		@ManyToOne(fetch = FetchType.LAZY)
	    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	    @JoinColumn(name="unitId")
		private LeaveType leaveType;

		protected Value() {};
		Value(Leave et, String systemId, String userId, Operation operation, String comment, Value value) {
			super(et, systemId, userId, operation, comment);
			this.leaveType = value.leaveType;			
			this.engagement = value.engagement;			
		}
		public Value(Engagement engagement, LeaveType leaveType) {
			this.engagement = engagement;
			this.leaveType = leaveType;
		}
		public Engagement getEngagement() {
			return engagement;
		}
		protected void setEngagement(Engagement engagement) {
			this.engagement = engagement;
		}
		public LeaveType getLeaveType() {
			return leaveType;
		}
		protected void setLeaveType(LeaveType leaveType) {
			this.leaveType = leaveType;
		}
		@Override
		public String diff(_Value<Leave, Bitemporal> otherValue, DateTime t0, DateTime t1) {
			StringBuffer sb = new StringBuffer();
			Value other = (Value)otherValue;
			
			Engagement.Bitemporal l0 = getEngagement().getValues().get(t0);
			Engagement.Bitemporal l1 = getEngagement().getValues().get(t1);

			if (!l0.getValue().getEntity().equals(l1.getValue().getEntity()) ||
				!l0.getValue().equals(l1.getValue())) {
				sb.append("Engagement.engagement: ");
//XXX:				sb.append('[').append(l0.getValue().getEntity().getUuid()).append("] ").append(l0.getValue().get).append(" - ");
//XXX:				sb.append('[').append(l0.getValue().getEntity().getUuid()).append("] ").append(l1.getValue().getAddress()).append('\n');
			}
			LeaveType.Bitemporal u0 = getLeaveType().getValues().get(t0);
			LeaveType.Bitemporal u1 = getLeaveType().getValues().get(t1);

			if (!u0.getValue().getEntity().equals(u1.getValue().getEntity()) ||
				!u0.getValue().equals(u1.getValue())) {
				sb.append("LeaveType.unit: ");
				sb.append('[').append(u0.getValue().getEntity().getUuid()).append("] ").append(u0.getValue().getName()).append(" - ");
				sb.append('[').append(u0.getValue().getEntity().getUuid()).append("] ").append(u1.getValue().getName()).append('\n');
			}
			
			return sb.toString();
		}
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append(super.toString());

			sb.append("  leaveType=").append(leaveType.getValues().get(getBitemporal().getValidityInterval().getStart()).getValue().getName()).append('\n');
//XXX:			sb.append("  unit=").append(unit.getValues().get(getBitemporal().getValidityInterval().getStart()).getValue().getName()).append('\n');
			
			return sb.toString();
		}
	}
	
	@Entity
	@Table(name = "PLeaveBitemporal")
	public static final class Bitemporal extends _Bitemporal<Bitemporal, Value> {
		private static final long serialVersionUID = -2132187534862370208L;
		
		protected Bitemporal() {}
	    private Bitemporal(Leave.Value value, Interval validityInterval) {
	        super(value, validityInterval);
	    }
	    
	    @Override
	    public Bitemporal copyWith(Interval validityInterval) {
	    	return new Bitemporal(value, validityInterval);
	    }
	}

	@SuppressWarnings("deprecation")
	@OneToMany(fetch = FetchType.LAZY)
    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name="LeaveId")
    private Collection<Engagement.Value> engagements = new LinkedList<Engagement.Value>();	

	public Leave() {
		super();
	}
	public Collection<Engagement.Value> getEngagements() {
		return engagements;
	}
	protected void setEngagements(Collection<Engagement.Value> engagements) {
		this.engagements = engagements;
	}
    public WrappedBitemporalProperty<Value, Bitemporal> getValues() {
    	return new WrappedBitemporalProperty<Value, Bitemporal>(values, new WrappedValueAccessor<Value,Bitemporal>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Bitemporal wrapValue(Value value, Interval validityInterval) {
				Bitemporal bi = new Bitemporal(value, validityInterval);
				return bi;
			}
    	});
    }
}
